﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlanningGIS.Win.Controls
{
    /// <summary>
    /// 根据数据类型改变输入的控件
    /// </summary>
    public partial class InputDataType : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public InputDataType()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 类型值：
        /// 0：字符串；
        /// 1：整数；
        /// 2：双精度；
        /// 3：时间；
        /// 4：是否；
        /// 5：IP；
        /// 6：枚举；
        /// 7：可扩展枚举
        /// </summary>
        private int _Type = 0;

        /// <summary>
        /// 输入的值
        /// </summary>
        private dynamic _Value = "";

        /// <summary>
        /// 获取输入的值
        /// </summary>
        public dynamic Value
        {
            get
            {
                _Value = GetValue();
                return _Value;
            }
            set { _Value = value; }
        }

        /// <summary>
        /// 设置数据源后绑定控件实际的值对应的属性名称
        /// </summary>
        public string ValueMember { get; set; }

        /// <summary>
        /// 设置数据源后绑定控件要显示的值对应的属性名称
        /// </summary>
        public string DisplayMember { get; set; }

        /// <summary>
        /// 数据源
        /// </summary>
        public dynamic DataSource { get; set; }

        /// <summary>
        /// 在扩展的数据类型下，当前输入的值是否是
        /// </summary>
        public bool Expanded
        {
            get
            {
                if (_Type == 7)
                {
                    if (cmbValue.SelectedValue == null) return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 当前值改变事件
        /// </summary>
        public event EventHandler ValueChanged = null;

        /// <summary>
        /// 按下Entry确认键
        /// </summary>
        public event EventHandler EntryPress = null;

        /// <summary>
        /// 初始化控件的数据类型
        /// </summary>
        /// <param name="type">数据类型</param>
        /// <param name="paramters"></param>
        public void Init(Type type, params object[] paramters)
        {
            if (type == typeof(string))
            {
                _Type = 0;
                txtValue.TextChanged -= new EventHandler(Value_Changed);
                txtValue.TextChanged += new EventHandler(Value_Changed);
                txtValue.KeyDown -= new KeyEventHandler(Entry_Down);
                txtValue.KeyDown += new KeyEventHandler(Entry_Down);
                txtValue.BringToFront();
            }
            else if (type == typeof(int) || type == typeof(Int16) || type == typeof(Int32) || type == typeof(Int64)
                 || type == typeof(long))
            {
                _Type = 1;
                intValue.ValueChanged -= new EventHandler(Value_Changed);
                intValue.ValueChanged += new EventHandler(Value_Changed);
                intValue.KeyDown -= new KeyEventHandler(Entry_Down);
                intValue.KeyDown += new KeyEventHandler(Entry_Down);
                intValue.BringToFront();
            }
            else if (type == typeof(double) || type == typeof(Single) || type == typeof(float))
            {
                _Type = 2;
                dblValue.ValueChanged -= new EventHandler(Value_Changed);
                dblValue.ValueChanged += new EventHandler(Value_Changed);
                dblValue.KeyDown -= new KeyEventHandler(Entry_Down);
                dblValue.KeyDown += new KeyEventHandler(Entry_Down);
                dblValue.BringToFront();
            }
            else if (type == typeof(DateTime))
            {
                _Type = 3;
                dtmValue.ValueChanged -= new EventHandler(Value_Changed);
                dtmValue.ValueChanged += new EventHandler(Value_Changed);
                dtmValue.KeyDown -= new KeyEventHandler(Entry_Down);
                dtmValue.KeyDown += new KeyEventHandler(Entry_Down);
                dtmValue.BringToFront();
            }
            else if (type == typeof(bool))
            {
                _Type = 4;
                chkValue.CheckedChanged -= new EventHandler(Value_Changed);
                chkValue.CheckedChanged += new EventHandler(Value_Changed);
                chkValue.KeyDown -= new KeyEventHandler(Entry_Down);
                chkValue.KeyDown += new KeyEventHandler(Entry_Down);
                chkValue.BringToFront();
            }
        }

        /// <summary>
        /// 初始化控件的数据类型
        /// </summary>
        /// <param name="type">数据类型</param>
        /// <param name="paramters"></param>
        public void Init(zjgisDataType type, params object[] paramters)
        {
            if (type == zjgisDataType.IP)
            {
                _Type = 5;
                ipValue.ValueChanged -= new EventHandler(Value_Changed);
                ipValue.ValueChanged += new EventHandler(Value_Changed);
                ipValue.KeyDown -= new KeyEventHandler(Entry_Down);
                ipValue.KeyDown += new KeyEventHandler(Entry_Down);
                ipValue.BringToFront();
            }
            else if (type == zjgisDataType.Enum || type == zjgisDataType.EnumExpand)
            {
                _Type = 6;
                cmbValue.SelectedIndexChanged -= new EventHandler(Value_Changed);
                cmbValue.SelectedIndexChanged += new EventHandler(Value_Changed);
                cmbValue.KeyDown -= new KeyEventHandler(Entry_Down);
                cmbValue.KeyDown += new KeyEventHandler(Entry_Down);
                cmbValue.BringToFront();
                if (!string.IsNullOrWhiteSpace(ValueMember)) cmbValue.ValueMember = ValueMember;
                if (!string.IsNullOrWhiteSpace(DisplayMember)) cmbValue.DisplayMember = DisplayMember;
                if (DataSource != null) cmbValue.DataSource = DataSource;
                cmbValue.DropDownStyle = ComboBoxStyle.DropDownList;
                if (type == zjgisDataType.EnumExpand)
                {
                    _Type = 7;
                    cmbValue.DropDownStyle = ComboBoxStyle.DropDown;
                }
            }
        }

        /// <summary>
        /// Entry键按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Entry_Down(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (EntryPress != null)
                    EntryPress(this, e);
            }
        }

        /// <summary>
        /// 控件的值改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Value_Changed(object sender, EventArgs e)
        {
            if (ValueChanged != null)
                ValueChanged(this, e);
        }

        /// <summary>
        /// 获取控件的值
        /// </summary>
        /// <returns></returns>
        private dynamic GetValue()
        {
            if (_Type == 0) return txtValue.Text.Trim();
            if (_Type == 1) return intValue.Value;
            if (_Type == 2) return dblValue.Value;
            if (_Type == 3) return dtmValue.Value;
            if (_Type == 4) return chkValue.Checked;
            if (_Type == 5) return ipValue.Value;
            if (_Type == 6) return cmbValue.Text;
            if (_Type == 7) return cmbValue.Text;
            return "";
        }

        /// <summary>
        /// 要输入的数据类型
        /// </summary>
        public enum zjgisDataType
        {
            /// <summary>
            /// IP地址
            /// </summary>
            IP,
            /// <summary>
            /// 枚举或数据集
            /// </summary>
            Enum,
            /// <summary>
            /// 可扩展的枚举或数据集
            /// </summary>
            EnumExpand

        }


    }
}
